/**
 * Created by DELL on 2021/3/26.
 */
layui.use(['form','layer','upload'], function(){
    var form = layui.form,
        $ = layui.$;


    $.ajax({
        type:"get",
        url:config.gatewayUri+"/"+config.deviceServiceId+"/api/manager/admin/getManagerList",
        data:{managertype:3},
        dataType:"json",
        success:function(result){
            if(result.code == 200){
                $("#sortingid").append(new Option("请选择",""));
                $.each(result.data,function(index,item){
                    if($("#sorting").val() == item.managerid){
                        $("#sortingid").append(new Option(item.username,item.managerid,true,true));
                    }else {
                        $("#sortingid").append(new Option(item.username, item.managerid));
                    }
                })
                layui.form.render("select");
            }else{
                layer.alert(result.msg);
            }
        },
        error:function(result){
            layer.alert(result);
        }
    });

    $.ajax({
        type:"get",
        url:config.gatewayUri+"/"+config.deviceServiceId+"/api/manager/admin/getManagerList",
        data:{managertype:4},
        dataType:"json",
        success:function(result){
            if(result.code == 200){
                $("#operatorsid").append(new Option("请选择",""));
                $.each(result.data,function(index,item){
                    if($("#operators").val() == item.managerid){
                        $("#operatorsid").append(new Option(item.username, item.managerid,true,true));
                    }else {
                        $("#operatorsid").append(new Option(item.username, item.managerid));
                    }
                })
                layui.form.render("select");
            }else{
                layer.alert(result.msg);
            }
        },
        error:function(result){
            layer.alert(result);
        }
    });

    $.ajax({
        type:"get",
        url:config.gatewayUri+"/"+config.deviceServiceId+"/api/venues/admin/getBackVenuesList",
        dataType:"json",
        success:function(result){
            if(result.code == 200){
                $("#venuesid").append(new Option("请选择",""));
                $.each(result.data,function(index,item){
                    if($("#venues").val() == item.venuesid){
                        $("#venuesid").append(new Option(item.venuesname, item.venuesid,true,true));
                    }else {
                        $("#venuesid").append(new Option(item.venuesname, item.venuesid));
                    }
                })
                layui.form.render("select");
            }else{
                layer.alert(result.msg);
            }
        },
        error:function(result){
            layer.alert(result);
        }
    });

    form.on('select(venuesid)', function(data){
        $("#position").html("");
        if(data.value != "") {
            $.ajax({
                type: "get",
                url: config.gatewayUri+"/"+config.deviceServiceId+"/api/venues/admin/getBackVenuesList",
                data: {venuesid: data.value},
                dataType: "json",
                success: function (result) {
                    if (result.code == 200) {
                        $.each(result.data, function (index, item) {
                            $("#position").append(new Option(item.province + "/" + item.city + "/" + item.area, item.province + "/" + item.city + "/" + item.area));
                        });
                        layui.form.render("select");
                        return;
                    } else {
                        layer.alert(result.msg);
                    }
                },
                error: function (result) {
                    layer.alert(result);
                }
            })
        };
        layui.form.render("select");
    });

    $("#sorting_partners").click(function(){
        $("#partnersid").html("");
        if($("#sortingid").val() != ""){
            $.ajax({
                type:"get",
                url:config.gatewayUri+"/"+config.deviceServiceId+"/api/partners/admin/getAllPartnersList",
                data:{fid:$("#sortingid").val()},
                dataType:"json",
                success:function(result){
                    if(result.data != ""){
                        $("#partnersid").append(new Option("请选择",""));
                        $.each(result.data,function(index,item){
                            $("#partnersid").append(new Option(item.username,item.partnersid));
                        });
                        $("#operators_partners").attr({"class":"layui-btn layui-btn-disabled","disabled":true})
                        layui.form.render("select");
                    }else{
                        layer.alert("当前分拣中心下没有合伙人");
                    }
                },
                error:function(result){
                    layer.alert(result);
                }
            });
        }else{
            layer.msg("请先选择分拣中心");
            $("#operators_partners").attr({"class":"layui-btn layui-btn-warm","disabled":false})
            layui.form.render("select");
        }
    })

    $("#operators_partners").click(function(){
        $("#partnersid").html("");
        if($("#operatorsid").val() != ""){
            $.ajax({
                type:"get",
                url:config.gatewayUri+"/"+config.deviceServiceId+"/api/partners/admin/getAllPartnersList",
                data:{fid:$("#operatorsid").val()},
                dataType:"json",
                success:function(result){
                    if(result.data != ""){
                        $("#partnersid").append(new Option("请选择",""));
                        $.each(result.data,function(index,item){
                            $("#partnersid").append(new Option(item.username,item.partnersid));
                        });
                        $("#sorting_partners").attr({"class":"layui-btn layui-btn-disabled","disabled":true})
                        layui.form.render("select");
                    }else{
                        layer.alert("当前运营商下没有合伙人");
                    }
                },
                error:function(result){
                    layer.alert(result);
                }
            });
        }else{
            layer.msg("请先选择运营商");
            $("#sorting_partners").attr({"class":"layui-btn layui-btn-warm","disabled":false})
            layui.form.render("select");
        }
    });

    $("#close").click(function(){
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);
    })

    $("form").submit(function(){
        var formData = new FormData();
        formData.append("deviceid",$("#deviceid").val());
        formData.append("devicenumber",$("#devicenumber").val());
        formData.append("devicename",$("#devicename").val());
        formData.append("rownumber",$("#rownumber").val());
        formData.append("columnnumber",$("#columnnumber").val());
        formData.append("stockmaxqty",$("#stockmaxqty").val());
        formData.append("stockflag",$("#stockflag").val());
        formData.append("sortingid",$("#sortingid").val());
        formData.append("sortinggrcomrate",$("#sortinggrcomrate").val());
        formData.append("sortinggoodscomrate",$("#sortinggoodscomrate").val());
        formData.append("operatorsid",$("#operatorsid").val());
        formData.append("operatorsgrcomrate",$("#operatorsgrcomrate").val());
        formData.append("partnersid",$("#partnersid").val());
        formData.append("partnersgrcomrate",$("#partnersgrcomrate").val());
        formData.append("partnersgoodscomrate",$("#partnersgoodscomrate").val());
        formData.append("venuesid",$("#venuesid").val());
        formData.append("position",$("#position").val());
        formData.append("address",$("#address").val());
        formData.append("isflag",$(".isflag").val());
        $.each($(".isflag"),function(index,object){
            if(object.checked == true){
                formData.append("isflag",object.value);
            }
        });
        $.ajax({
            url:config.gatewayUri+"/"+config.deviceServiceId+"/api/device/admin/saveOrUpdateDevice",
            data:formData,
            processData:false,
            contentType:false,
            type:'post',
            dataType:'json',
            success:function(res){
                if(res.code == 200){
                    layer.alert("操作成功!");
                    setTimeout(close,1000);
                }
            }
        })
    });

    function close() {
        window.parent.location.reload();//刷新父页面
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);
    }
});